<template>
    <div class="header">
        <div class="logo">
            <div class="logo-content">
                <i class="bike-icon">🚲</i>
            </div>
        </div>
        <div class="user-area" @click="handleUserClick">
            <template v-if="user">
                <div class="avatar">
                    {{ user.charAt(0).toUpperCase() }}
                </div>
                <span class="username">{{ user }}</span>
            </template>
            <template v-else>
                <div class="login-btn">
                    登录/注册
                </div>
            </template>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { getUserInfo } from "@/api/index.js";
import { useRouter } from 'vue-router';

const router = useRouter();
const user = ref('');

onMounted(() => {
    getUserInfo().then(res => {
        user.value = res.data.username;
    }).catch(() => {
        user.value = '';
    });
});

const handleUserClick = () => {
    if (!user.value) {
        router.push('/login');
    }
    // 如果已登录，可以添加用户菜单弹出逻辑
};
</script>

<style lang="css" scoped>
.header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 56px;
    background: #409EFF;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 16px;
    padding-top: env(safe-area-inset-top);
    z-index: 1000;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.logo {
    height: 36px;
    display: flex;
    align-items: center;
}

.logo-content {
    display: flex;
    align-items: center;
    gap: 8px;
}

.bike-icon {
    font-size: 24px;
    animation: ride 0.5s ease-out;
}

.logo-text {
    color: white;
    font-size: 16px;
    font-weight: 600;
    letter-spacing: 1px;
}

.user-area {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    padding: 4px;
    border-radius: 20px;
    transition: all 0.3s ease;
}

.user-area:active {
    opacity: 0.8;
}

.avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.2);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    font-weight: 500;
    border: 2px solid rgba(255, 255, 255, 0.5);
}

.username {
    color: white;
    font-size: 14px;
    font-weight: 500;
    max-width: 100px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.login-btn {
    color: white;
    font-size: 14px;
    padding: 6px 12px;
    border-radius: 16px;
    background: rgba(255, 255, 255, 0.2);
    transition: all 0.3s ease;
}

.login-btn:active {
    background: rgba(255, 255, 255, 0.3);
}

/* 适配刘海屏 */
@supports (padding-top: env(safe-area-inset-top)) {
    .header {
        height: calc(56px + env(safe-area-inset-top));
    }
}

/* 添加动画效果 */
@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(-10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.user-area {
    animation: fadeIn 0.3s ease-out;
}

/* 添加自行车图标动画 */
@keyframes ride {
    0% {
        transform: translateX(-20px) rotate(-10deg);
        opacity: 0;
    }
    100% {
        transform: translateX(0) rotate(0deg);
        opacity: 1;
    }
}

/* 禁用文本选择 */
* {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

/* 适配深色模式 */
@media (prefers-color-scheme: dark) {
    .header {
        background: #1890ff;
    }
    
    .avatar {
        background: rgba(255, 255, 255, 0.15);
    }
    
    .login-btn {
        background: rgba(255, 255, 255, 0.15);
    }
}
</style>